package com.monkeygroup.mygoplus.view.home.activity.person;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.base.BaseActivity;
import com.monkeygroup.mygoplus.common.utils.VilidateUtil;
import com.monkeygroup.mygoplus.http.HttpModel;
import com.monkeygroup.mygoplus.http.RequestUrl;
import com.monkeygroup.mygoplus.user.vo.PersonalNomalInfo;
import com.monkeygroup.mygoplus.view.entrance.fragment.RegitsterCodeFragment;

import java.util.HashMap;
import java.util.Map;

/**
 * 個人電話
 * Created by cumt on 2017/6/20.
 */

public class PersonChangeTelphoneActivity extends BaseActivity implements View.OnClickListener {

    public static void gotoActivity(Activity activity,String telephone) {
        Intent intent = new Intent(activity,
                PersonChangeTelphoneActivity.class);

        intent.putExtra("telephone",telephone);
        activity.startActivityForResult(intent,300);
    }

    private Activity activity;

    private ImageView imgBack;
    private TextView txtTitle;

    private EditText et_new_telephone;
    private TextView btn_change_telephone_ok,code_time;

    private LinearLayout ll_sending;
    private RelativeLayout rel_sended;

    private String telephone;
    //請求參數
    private Map<String, String> params;
    private HttpModel httpModel;

    private PersonalNomalInfo loginuserinfo;


    @Override
    public void onClick(View v) {

    }

    @Override
    public void setContentView() {
        setContentView(R.layout.person_change_telephone);
    }

    @Override
    public void initView() {


        activity= PersonChangeTelphoneActivity.this;

        imgBack = (ImageView) findViewById(R.id.img_back);
        txtTitle = (TextView) findViewById(R.id.txt_title);
        txtTitle.setText("变更手機號碼");

        et_new_telephone = (EditText)findViewById(R.id.et_new_telephone);
        btn_change_telephone_ok= (TextView)findViewById(R.id.btn_change_telephone_ok);

        rel_sended= (RelativeLayout)findViewById(R.id.rel_sended);
        ll_sending= (LinearLayout)findViewById(R.id.ll_sending);


        code_time= (TextView)findViewById(R.id.code_time);
        params = new HashMap<>();

        Intent intent = this.getIntent();
        Bundle bundle =  intent.getExtras();
        telephone= bundle.getString("telephone");


        TextWatcher cname=new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if(VilidateUtil.isMobile(String.valueOf(et_new_telephone.getText()))){
                    params.put("modifyMobile",String.valueOf(et_new_telephone.getText()));
                }


            }

            @Override
            public void afterTextChanged(Editable s) {



            }
        };

        et_new_telephone.addTextChangedListener(cname);

        initRequest();

    }

    @Override
    public void setListener() {

    }

    @Override
    public void doEvent() {

        btn_change_telephone_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(VilidateUtil.isMobile(String.valueOf(et_new_telephone.getText()))){
                    httpModel.startRequest();
                }
                else{
                    Toast.makeText(activity, "請輸入正確的手機號",
                            Toast.LENGTH_SHORT).show();

                }

            }
        });

    }

    private void initRequest() {

        //发送短信
        httpModel = new HttpModel(RequestUrl.baseUrl+ RequestUrl.verifyUrl, params, new HttpModel.SimpleOnHttpRequestListener() {
            @Override
            public void onRequestFinished(int resultCode, String resultMsg, Object data) {

                if (!VilidateUtil.isSuccess(resultCode)) {

//                    MyAlertDialog dialog = new MyAlertDialog(activity, resultMsg, 0);
//                    dialog.show(activity, resultMsg, 0);

                    //  code=(String)data;
                    PersonInputCodeActivity.gotoActivity(activity,String.valueOf(et_new_telephone.getText()));
                } else {
               //     time.start();

                    Toast.makeText(activity, "發送失敗",
                            Toast.LENGTH_SHORT).show();
                }
            }
        }, true);

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 300 && data != null) {

            String phone = data.getStringExtra("phone");
            final Intent intent=new Intent();
            intent.putExtra("phone",phone);
            activity.setResult(RESULT_OK,intent);
            finish();

        }

    }


}
